* This code replicates the main results in Tables 1A, 2A, 3A, 4, 5, 6, and 9 of 
* Product Innovation and Credit Market Disruptions (forthcoming RFS)
* by Joao Granja and Sara Moreira
*
* The main data set is mainsample_placebo.dta
* This is a pseudo data set that illustrates the format of the original data set

clear all
set more off

use "mainsample_placebo.dta", clear
tsset firm year

// Step 1) Defining Crisis Years
gen crisis = year == 2008 | year == 2009 | year == 2010

// Step 2) Defining Fixed Effects and Control Variables
gen sblshock = -bartik
gen lnrev = ln(1+rev_it)
gen laglnrev = l.lnrev
gen lagavrev = ln(l.rev_mean_it)
xtile rev = l.rev_it, nq(10)
xtile nprod = l.T2_it, nq(10)

global a laglnrev hhi_product_it lagavrev

// Label Variables
label var N_it "Nbr. of New Products (All)"
label var Na_it "Nbr. of New Products (Old)"
label var Nf_it "Nbr. of New Products (New)"
label var product_intro "I(New Prod. =1) (All)"
label var product_intro_a "I(New Prod. =1) (Old)"
label var product_intro_b "I(New Prod. =1) (New)"
label var T2_it "Total Nbr. of Products"
label var modules_it "Total Nbr. of Product Mod."
label var groups_it "Total Nbr. of Product Groups"
label var rev_r_it "Total Annual Revenues"
label var sblshock "Local Credit Shock"
label var crisis "I(Crisis)"
label var lnrev "Ln(Firm Revenue)"
label var laglnrev "Ln(Firm Revenue)"
label var hhi_product_it "Rev. Concentration Index"
label var lagavrev "Ln(Rev. per Product)"

// Winsorize  Variables
winsor2 entryrate entryrate_a entryrate_b entryrate_c , replace cuts(1 99)

********************************************************************************
// Table 1: Panel A: Summary Statistics Full Sample
********************************************************************************

eststo: estpost tabstat sblshock N_it Na_it Nf_it product_intro product_intro_a product_intro_b T2_it modules_it groups_it rev_r_it if entryrate!=., s(N mean sd q) c(stat)
*...to latex...
esttab using "Table1A.tex", cells("count(fmt(a0)) mean(fmt(a3)) sd(fmt(a3)) p25(fmt(a3)) p50(fmt(a3)) p75(fmt(a3)) ") /// 
label nodepvars nomtitles noobs plain lines fragment drop() noomit width(\hsize) replace
eststo clear


********************************************************************************
// Table 2: Panel A: Product Innovation: Evolution over Time
********************************************************************************

preserve
drop if year==2006
collapse entryrate entryrate_a entryrate_b product_intro product_intro_a product_intro_b, by(year)
tsset year

mkmat _all, matrix(b)
mat colnames b=Year "Entry Rate (All Lines)"  "Entry Rate (Old Lines)" "Entry Rate (New Lines)" "I(New Product=1) (All Lines)" "I(New Product=1) (Old Lines)" "I(New Product=1) (New Lines)"
outtable using "Table2A", mat(b) replace nobox center f(%9.0f %12.3fc %12.3fc %12.3fc %12.3fc %12.3fc %12.3fc) norowlab
restore

********************************************************************************
// Table 3 Panel A: Product Innovation and Credit-Market Disruption (Geographic Variation)
********************************************************************************

eststo: reghdfe entryrate c.sblshock#c.crisis if lagavrev!=., absorb(firm year nprod) vce(cl st)
eststo: reghdfe entryrate c.sblshock#c.crisis $a, absorb(firm year nprod rev) vce(cl st)
eststo: reghdfe entryrate_a c.sblshock#c.crisis if lagavrev!=., absorb(firm year nprod) vce(cl st)
eststo: reghdfe entryrate_a c.sblshock#c.crisis $a, absorb(firm year nprod rev) vce(cl st)
eststo: reghdfe entryrate_b c.sblshock#c.crisis if lagavrev!=., absorb(firm year nprod) vce(cl st)
eststo: reghdfe entryrate_b c.sblshock#c.crisis $a, absorb(firm year nprod rev) vce(cl st)
  
esttab using "Table3A.tex", ///
nodepvars nomti nonum collabels(none) ///
label b(3) se(3) parentheses star(* 0.10 ** 0.05 *** 0.01) ar2   ///
interaction(" $\times$ ") replace plain lines fragment drop() noomit
eststo clear

********************************************************************************
// Table 4: Product Innovation and Credit-Market Disruption: Large Share of Sales Outside Home State
********************************************************************************

// Panel A: Panel A: 75% sales outside Home State
eststo: reghdfe entryrate c.sblshock#c.crisis if  outsharerev>.75 & outsharerevenue!=., absorb(firm year nprod) vce(cl st)
eststo: reghdfe entryrate c.sblshock#c.crisis $a if  outsharerev>.75 & outsharerevenue!=., absorb(firm year nprod rev) vce(cl st)
eststo: reghdfe entryrate_a c.sblshock#c.crisis if  outsharerev>.75 & outsharerevenue!=., absorb(firm year nprod) vce(cl st)
eststo: reghdfe entryrate_a c.sblshock#c.crisis $a if  outsharerev>.75 & outsharerevenue!=., absorb(firm year nprod rev) vce(cl st)
eststo: reghdfe entryrate_b c.sblshock#c.crisis if  outsharerev>.75 & outsharerevenue!=., absorb(firm year nprod) vce(cl st)
eststo: reghdfe entryrate_b c.sblshock#c.crisis $a if outsharerev>.75 & outsharerevenue!=., absorb(firm year nprod rev) vce(cl st)

esttab using "Table4A.tex", ///
nodepvars nomti nonum collabels(none) ///
label b(3) se(3) parentheses star(* 0.10 ** 0.05 *** 0.01) ar2   ///
interaction(" $\times$ ") replace plain lines fragment drop($a) noomit
eststo clear

// Panel B: 75% sales outside Home State + Sales in more than 25 states
eststo: reghdfe entryrate c.sblshock#c.crisis if lagavrev!=. & nstates>25 & outsharerev>.75 & outsharerevenue!=., absorb(firm year nprod) vce(cl st)
eststo: reghdfe entryrate c.sblshock#c.crisis $a if nstates>25 & outsharerev>.75 & outsharerevenue!=., absorb(firm year nprod rev) vce(cl st)
eststo: reghdfe entryrate_a c.sblshock#c.crisis if lagavrev!=. & nstates>25 & outsharerev>.75 & outsharerevenue!=., absorb(firm year nprod) vce(cl st)
eststo: reghdfe entryrate_a c.sblshock#c.crisis $a if nstates>25 & outsharerev>.75 & outsharerevenue!=., absorb(firm year nprod rev) vce(cl st)
eststo: reghdfe entryrate_b c.sblshock#c.crisis if lagavrev!=. & nstates>25 & outsharerev>.75 & outsharerevenue!=., absorb(firm year nprod) vce(cl st)
eststo: reghdfe entryrate_b c.sblshock#c.crisis $a if nstates>25 & outsharerev>.75 & outsharerevenue!=., absorb(firm year nprod rev) vce(cl st)

esttab using "Table4B.tex", ///
nodepvars nomti nonum collabels(none) ///
label b(3) se(3) parentheses star(* 0.10 ** 0.05 *** 0.01) ar2   ///
interaction(" $\times$ ") replace plain lines fragment drop($a) noomit
eststo clear

sum HHIrev, de
local a = `r(p50)'

eststo: reghdfe entryrate c.sblshock#c.crisis if lagavrev!=. & nstates>25 & HHIrev<`a' & outsharerev>.75 & outsharerevenue!=., absorb(firm year nprod) vce(cl st)
eststo: reghdfe entryrate c.sblshock#c.crisis $a if nstates>25 & HHIrev<`a'& outsharerev>.75 & outsharerevenue!=., absorb(firm year nprod rev) vce(cl st)
eststo: reghdfe entryrate_a c.sblshock#c.crisis if lagavrev!=. & nstates>25 & HHIrev<`a' & outsharerev>.75 & outsharerevenue!=. , absorb(firm year nprod) vce(cl st)
eststo: reghdfe entryrate_a c.sblshock#c.crisis $a if nstates>25 & HHIrev<`a' & outsharerev>.75 & outsharerevenue!=., absorb(firm year nprod rev) vce(cl st)
eststo: reghdfe entryrate_b c.sblshock#c.crisis if lagavrev!=. & nstates>25 & HHIrev<`a'  & outsharerev>.75 & outsharerevenue!=., absorb(firm year nprod) vce(cl st)
eststo: reghdfe entryrate_b c.sblshock#c.crisis $a if nstates>25 & HHIrev<`a' & outsharerev>.75 & outsharerevenue!=. , absorb(firm year nprod rev) vce(cl st)

esttab using "Table4C.tex", ///
nodepvars nomti nonum collabels(none) ///
label b(3) se(3) parentheses star(* 0.10 ** 0.05 *** 0.01) ar2   ///
interaction(" $\times$ ") replace plain lines fragment drop($a) noomit
eststo clear

********************************************************************************
// Table 5: Product Innovation and Credit-Market Disruption (Geographic Variation)
********************************************************************************

sum debtdep, de
gen hidebtdep = debtdep>`r(p50)' if debtdep!=.

egen deltamodule = sd(group_trip_new), by(firm)

egen size_2007 = mean(rev_it)  if year==2007 | year ==2006 | year==2008, by(firm)
egen size = mean(size_2007), by(firm)
drop size_2007
gen hisize = size > 418540.9 if size!=.

gen semidura = 1 - group_trip_new

label var lcensored_i "Old Firm"
label var semidura "Semi-Durable Product"
label var hidebtdep "Hi. Debt Dep."
label var hisize "Large Firm"

eststo: reghdfe entryrate_b c.sblshock##c.crisis##c.lcensored_i , absorb(firm year nprod ) vce(cl st)
eststo: reghdfe entryrate_b c.sblshock##c.crisis##c.lcensored_i $a, absorb(firm year nprod rev) vce(cl st)
eststo: reghdfe entryrate_b c.sblshock##c.crisis##c.hisize , absorb(firm year nprod) vce(cl st)
eststo: reghdfe entryrate_b c.sblshock##c.crisis##c.hisize  $a, absorb(firm year nprod rev) vce(cl st)
eststo: reghdfe entryrate_b c.sblshock##c.crisis##c.semidura if deltamodule==0  , absorb(firm year nprod ) vce(cl st)
eststo: reghdfe entryrate_b c.sblshock##c.crisis##c.semidura $a if deltamodule==0 , absorb(firm year nprod rev) vce(cl st)
eststo: reghdfe entryrate_b c.sblshock##c.crisis##c.hidebtdep , absorb(firm year nprod ) vce(cl st)
eststo: reghdfe entryrate_b c.sblshock##c.crisis##c.hidebtdep  $a , absorb(firm year nprod rev) vce(cl st)

esttab using "Table5.tex", ///
nodepvars nomti nonum collabels(none) ///
label b(3) se(3) parentheses star(* 0.10 ** 0.05 *** 0.01) ar2   ///
interaction(" $\times$ ") replace plain lines fragment drop($a) noomit
eststo clear

********************************************************************************
// Table 6: Credit-Market Disruption and Novelty of Product Innovation
********************************************************************************

foreach var of varlist newness_index_imt newness_indexC_imt HN_weightedA {
winsor2 newness_index_imt  newness_indexC_imt HN_weightedA, cuts(0 99) replace
gen ln`var' = ln(1+ `var') if HN_weightedA!=. & newness_index_imt!=. & newness_indexC_imt!=.
}

preserve
foreach var of varlist newness_index_imt newness_indexC_imt HN_weightedA {
eststo: reghdfe ln`var' c.sblshock#c.crisis if lagavrev!=. , absorb(firm year nprod) vce(cl st) 
eststo: reghdfe ln`var' c.sblshock#c.crisis $a, absorb(firm year nprod rev) vce(cl st) 
}

esttab using "Table6.tex", ///
nodepvars nomti nonum collabels(none) ///
label b(3) se(3) parentheses star(* 0.10 ** 0.05 *** 0.01) ar2   ///
interaction(" $\times$ ") replace plain lines fragment drop() noomit
eststo clear
restore

********************************************************************************
// Table 9 -- Aggregation Exercise Discussed in section 6 of the main draft
********************************************************************************
	
    ****************************************************************************
    * Step 1. Loop over counties and compute total effect.
    ****************************************************************************
	preserve 
	tempfile cnty
    duplicates drop stcntybr, force
    xtile exposure_tally = sblshock, n(10)
    keep stcntybr exposure_tally
    save `cnty'
	restore

    merge m:1 stcntybr using `cnty', keep(3) nogen
	fastxtile prodrank = T2_it if year==2007, nq(100)
	gen totproductsinnewmodules = N_it - Na_it - Ne_it
	
	
	
	foreach num of numlist 2007 2010{
	sum totproductsinnewmodules if year==`num'
	local prod`num' = `r(sum)'
	}

	local prod_delta = `prod2010' - `prod2007' 
	di `prod_delta'
	
	local evar sblshock
	sum `evar' if exposure_tally == 1, meanonly
    local exp_baseline_cons = 0
    local exp_baseline_aggr = `r(mean)'

    ****************************************************************************
    * Step 1.1. Compute coefficient of interest 
    ****************************************************************************

	local evar sblshock
	reghdfe entryrate_b c.sblshock#c.crisis $a, absorb(firm year nprod rev) vce(cl st)  
    local coeff = _b[c.sblshock#c.crisis]	
	
	****************************************************************************
    * Step 2. Extract results into logfile.
    ****************************************************************************
	
	* Step 2.1
    local outfile "Table9.txt"
    local induced = 0
	local induced_aggr = 0
    forvalues i = 1/10 {
        sum `evar' if exposure_tally == `i', meanonly
		local exp = max(`r(mean)',0)
        local exp_impact = `coeff' * (`exp' - `exp_baseline_cons')
		local exp_impact_aggr = `coeff' * (`r(mean)' - `exp_baseline_aggr')
        sum T2_it if year == 2007 & exposure_tally == `i' & prodrank<100, meanonly
        local induced = `induced' + `exp_impact' * `r(sum)'
		local induced_aggr = `induced_aggr' + `exp_impact_aggr' * `r(sum)'
    }
	
    !echo "  Induced: `induced', Induced (Agressive): `induced_aggr', Decline in New Products in New Lines (2007 - 2010): `prod_delta'" >> `outfile'
    !echo "" >> `outfile'

    local induced = 0
	local induced_aggr = 0
    forvalues i = 1/10 {
        sum `evar' if exposure_tally == `i', meanonly
		local exp = max(`r(mean)',0)
        local exp_impact = `coeff' * (`exp' - `exp_baseline_cons')
		local exp_impact_aggr = `coeff' * (`r(mean)' - `exp_baseline_aggr')
        sum T2_it if year == 2007 & exposure_tally == `i' & prodrank<96, meanonly
        local induced = `induced' + `exp_impact' * `r(sum)'
		local induced_aggr = `induced_aggr' + `exp_impact_aggr' * `r(sum)'
    }
	
	
    !echo "  Induced: `induced', Induced (Agressive): `induced_aggr',  Decline in New Products in New Lines (2007 - 2010): `prod_delta'" >> `outfile'
    !echo "" >> `outfile'

    local induced = 0
	local induced_aggr = 0
    forvalues i = 1/10 {
        sum `evar' if exposure_tally == `i', meanonly
		local exp = max(`r(mean)',0)
        local exp_impact = `coeff' * (`exp' - `exp_baseline_cons')
		local exp_impact_aggr = `coeff' * (`r(mean)' - `exp_baseline_aggr')
        sum T2_it if year == 2007 & exposure_tally == `i' & prodrank<91, meanonly
        local induced = `induced' + `exp_impact' * `r(sum)'
		local induced_aggr = `induced_aggr' + `exp_impact_aggr' * `r(sum)'
    }


	
    !echo "  Induced: `induced', Induced (Aggressive): `induced_aggr', Decline in New Products in New Lines (2007 - 2010): `prod_delta'" >> `outfile'
    !echo "" >> `outfile'

